package leetcode.code1805;

import java.util.HashMap;

public class Solution2 {

	class Node extends HashMap<Character, Node> {
		boolean end;
	}

	public int numDifferentIntegers(String word) {
		Node root = new Node();
		Node node = null;
		int ans = 0;
		for (int i = 0, num = 0; i <= word.length(); i++) {
			char c = i < word.length() ? word.charAt(i) : ' ';
			if (c >= '0' && c <= '9') {
				if (node == null) {
					node = root;
				}
				num = num * 10 + c - '0';
				if (num != 0) {
					node = node.computeIfAbsent(c, key -> new Node());
				}
			} else if (node != null) {
				if (num == 0) {
					node = node.computeIfAbsent('0', key -> new Node());
				}
				if (!node.end) {
					ans++;
				}
				node.end = true;
				node = null;
				num = 0;
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.numDifferentIntegers("a1b01c001");
	}
}
